//
//  News24HoursTableView.m
//  item-NetEaseNews-tyw
//
//  Created by tyw on 15/10/1.
//  Copyright (c) 2015年 tyw. All rights reserved.
//

#import "News24HoursTableView.h"
#import "NewsModel.h"
#import "News24HoursTableViewCell.h"

#import "News24HoursViewController.h"
#import "PhotoShowViewController.h"
#import "NewsDetailViewController.h"

#define navImgViewHeight 260.0/828.0*kWidth      //顶部背景图片的高度

@implementation News24HoursTableView {
    UIImageView* navImgView;            //位于顶部的背景图,100
    UIImageView* titleImgview;          //位于顶部的“24小时要闻”字样的图片,101
    UILabel* introduceLabel;            //位于顶部的“聚焦今日时事 浓缩新闻精华”字样,102
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        
        self.delegate = self;
        self.dataSource = self;
        
        self.backgroundColor = [UIColor clearColor];
        
        [self registerClass:[News24HoursTableViewCell class] forCellReuseIdentifier:kNews24HoursTableViewCellId];
    }
    
    return self;
}

#pragma mark - 数据的set方法
- (void)setNewsModelArray:(NSArray *)newsModelArray {
    _newsModelArray = [newsModelArray copy];
    
    [self reloadData];
}

#pragma mark - tableView协议方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _newsModelArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    News24HoursTableViewCell* cell = [self dequeueReusableCellWithIdentifier:kNews24HoursTableViewCellId forIndexPath:indexPath];
    
    NewsModel* model = [[NewsModel alloc] initWithDataDic:_newsModelArray[indexPath.row]];
    cell.newsTag = indexPath.row;
    cell.newsModel = model;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {          //头条
        return 300;
    } else {                //非头条
        return 100;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    //通过响应者链,找到News24HoursViewController
    News24HoursViewController* news24VC = [Tools className:@"News24HoursViewController" fromResponder:self.nextResponder];
    
    if (news24VC) {
        NewsModel* model = [[NewsModel alloc] initWithDataDic:_newsModelArray[indexPath.row]];
        NewsDetailViewController* newsDetailVC = [[NewsDetailViewController alloc] init];
        if (model.newsId.length) {
            newsDetailVC.newsId = model.newsId;
            [news24VC.navigationController pushViewController:newsDetailVC animated:YES];
        }
    }
}

#pragma mark - scrollView协议方法
//实时监控表视图滑动事件，设置一波
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    //响应者链找到控制器，再找到各个视图    
    News24HoursViewController* news24VC = [Tools className:@"News24HoursViewController" fromResponder:self.nextResponder];
    
    if (news24VC) {
        //位于顶部的背景图,100
        navImgView = (UIImageView*)[news24VC.view viewWithTag:100];
        
        //位于顶部的“24小时要闻”字样的图片,101
        titleImgview = (UIImageView*)[news24VC.view viewWithTag:101];
        
        //位于顶部的“聚焦今日时事 浓缩新闻精华”字样,102
        introduceLabel = (UILabel*)[news24VC.view viewWithTag:102];
    }
    
    CGFloat offY = scrollView.contentOffset.y;
    [self resetViewsTransform:offY];
}

//将要停止减速时，再来设置一波
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
    CGFloat offY = scrollView.contentOffset.y;
    [self resetViewsTransform:offY];
}

//滑动视图将要停止在某一个位置再来设置一波
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
    
    CGFloat offY = targetContentOffset->y;
    [self resetViewsTransform:offY];
}

- (void)resetViewsTransform:(CGFloat)offY {
    //滑动了多少
    CGFloat yy = offY+20;
    
    if (offY > -20 && offY < navImgViewHeight-navBarHeight-20) {
        navImgView.transform = CGAffineTransformMakeTranslation(0, -offY-20);
        
        if (yy > 0 && yy < 20) {
            introduceLabel.transform = CGAffineTransformMakeTranslation(0, -offY-20);
            introduceLabel.alpha = 1.0/yy*2;
#warning transform同时实现平移缩放旋转
            CGFloat scale = 1-yy/60;
            titleImgview.transform = CGAffineTransformMake(scale, 0, 0, scale, 0, -offY-20);
        }
    } else if (offY == -20) {               //当停止减速时，如果在原点，就全恢复
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.3];
        introduceLabel.transform = CGAffineTransformIdentity;
        titleImgview.transform = CGAffineTransformIdentity;
        navImgView.transform = CGAffineTransformIdentity;
        introduceLabel.alpha = 1;
        [UIView commitAnimations];
    } else if (offY < -20) {
    } else {
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.3];
        navImgView.transform = CGAffineTransformMakeTranslation(0, -navImgViewHeight+navBarHeight);
        introduceLabel.transform = CGAffineTransformMakeTranslation(0, -20);
        introduceLabel.alpha = 0;
        titleImgview.transform = CGAffineTransformMake(2.0/3, 0, 0, 2.0/3, 0, -20);
        [UIView commitAnimations];
    }
}

@end
